import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'
import Topnav from '../components/Topnav'
import axios from '../lib/http'
Vue.use(VueRouter)
const routes = [{
    path: '/',
    name: 'Home',
    components: {
      default: Home,
      Topnav: Topnav
    },
    meta:{
      title:"首页",
      auth:true,
      authLV:[1,2]
    }
  },
  {
    path: '/about',
    name: 'About',
    component: () => import('../views/About.vue'),
    meta:{
      title:"输入",
      auth:true,
      authLV:[1,2]
    }
  },
  {
    //登入
    path: '/login',
    name: 'Login',
    component: () => import('../views/Login.vue'),
    meta:{
      title:"登入",
      auth:false,
      authLV:[1,2]
    }
  },
  {
    //注册
    path: '/register',
    name: 'Register',
    component: () => import('../views/Register.vue'),
    meta:{
      title:"注册",
      auth:false,
      authLV:[1,2]
    }
  },
  {
    //个人中心
    path: '/usercenter',
    name: 'Usercenter',
    components: {
      default: () => import('../views/Usercenter.vue'),
      Topnav: Topnav
    },
    meta:{
      title:"个人中心",
      auth:true,
      authLV:[1,2]
    }
  },
  {
     //发布
    path: '/post',
    name: 'Post',
    components: {
      default: () => import('../views/Post.vue'),
      Topnav: Topnav
    },
    meta:{
      title:"发布",
      auth:true,
      authLV:[1,2]
    }
  },
  {
      //内容详情
    path: '/detail',
    name: 'Detail',
    components: {
      default: () => import('../views/Detail.vue'),
      Topnav: Topnav
    },
    meta:{
      title:"内容详情",
      auth:true,
      authLV:[1,2]
    }
  },
  {
    //分类
    path: '/classify',
    name: 'Classify',
    components: {
      default: () => import('../views/Classify.vue'),
      Topnav: Topnav
    },
    meta:{
      title:"分类",
      auth:true,
      authLV:[1,2]
    }

  }


]

const router = new VueRouter({
  mode: "hash",
  base: process.env.BASE_URL,
  // base:"/dist/",
  routes
})
 router.beforeEach((to, from, next) => {
   if (to.meta.auth) {
     //判断是否在的登入状态
    axios.get("/api/isLogin.php").then(res=>{
      if (res.data.ret==200) {
        next()
      }else{
        next({name:"Login"})
      }
    }).catch(res=>[
      console.log(res)
    ]) 
   }
  if (to.meta.title) {
    document.title=to.meta.title
  }
  next()
})
export default router